 

package com.suzj.extraenchantments.util;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;

import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import javax.annotation.Nullable;
 
public class GetEntityHelper {
    public static AABB getBoundingBoxAroundEntity(Entity entity, double radius) {
        return new AABB(entity.getX() - radius, entity.getY() - radius, entity.getZ() - radius, entity.getX() + radius, entity.getY() + radius, entity.getZ() + radius);
    }
    public static double distanceCount(Entity e, double x, double y, double z) {
        double d0 = e.getX() - x;
        double d1 = e.getY() - y;
        double d2 = e.getZ() - z;
        return (d0 * d0) + (d1 * d1) + (d2 * d2);
    }
    @Nullable
    public static <T extends LivingEntity> T getClosestEntity(List<? extends T> entities, Predicate<LivingEntity> predicate, double x, double y, double z) {
        double d0 = (double)-1.0F;
        T t = null;

        for(T t1 : entities) {
            if (predicate.test(t1)) {
                double d1 =  distanceCount(t1, x, y, z);
                if (d0 == (double)-1.0F || d1 < d0) {
                    d0 = d1;
                    t = t1;
                }
            }
        }

        return t;
    }

    public static Player getNearestPlayer(Entity entity) {
        Level world = entity.getCommandSenderWorld();
        if (world == null) {
            return null;
        } else {
            List<Player> players = (List<Player>) world.players();
            return (Player)players.stream().min(Comparator.comparingDouble((player) -> (double)player.distanceTo(entity))).orElse(null);
        }
    }

    public static ItemStack createFireworkItem() {
        ItemStack fireworkItem = new ItemStack(Items.FIREWORK_ROCKET);
        CompoundTag CompoundTag = new CompoundTag();
        CompoundTag fireworksCompound = new CompoundTag();
        fireworksCompound.putByte("Flight", (byte)1);
        ListTag explosions = new ListTag();
        CompoundTag explosion = new CompoundTag();
        explosion.putBoolean("Flicker", false);
        explosion.putBoolean("Trail", false);
        explosion.putBoolean("Twinkle", false);
        explosion.putIntArray("Colors", new int[]{(int)(Math.random() * (double)1.6777215E7F)});
        explosion.putIntArray("FadeColors", new int[]{(int)(Math.random() * (double)1.6777215E7F)});
        explosion.putByte("Type", (byte)((int)(Math.random() * (double)5.0F)));
        explosions.add(explosion);
        fireworksCompound.put("Explosions", explosions);
        CompoundTag.put("Fireworks", fireworksCompound);
        fireworkItem.setTag(CompoundTag);
        return fireworkItem;
    }
}
